package com.hm.ibsm.service;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Component;

import com.hm.common.def.DataStatusDef;
import com.hm.common.exception.ErrorCode;
import com.hm.common.exception.ServiceException;
import com.hm.common.su.bean.PageInfo;
import com.hm.common.su.bean.PageInfo.PageParam;
import com.hm.common.util.CommonUtil;
import com.hm.common.util.StringUtil;
import com.hm.ibsm.domain.mg.QRoleDomain;
import com.hm.ibsm.domain.mg.RoleDomain;
import com.hm.ibsm.repositry.IRoleRepository;
import com.hm.ibsm.util.BaseDomain;
import com.hm.ibsm.util.BeanUtil;
import com.hm.ibsm.util.R.DB;
import com.hm.ibsm.vo.role.RoleVo;
import com.querydsl.core.types.dsl.BooleanExpression;

/**
 * @author shishun.wang
 * @date 2017年2月10日 上午1:59:38
 * @version 1.0
 * @describe
 */
@Component
@Transactional
public class RoleService {

	@Autowired
	private IRoleRepository roleRepository;

	public void addRole(RoleVo role) {
		if (CommonUtil.isEmpty(role.getRoleName())) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_NULL, "角色名称不能为空");
		}

		RoleDomain domain = roleRepository.getRoleByRoleName(role.getRoleName());
		if (CommonUtil.isNotEmpty(domain)) {
			throw ServiceException.warn("该角色名称已经被使用");
		}

		RoleDomain roleDomain = new RoleDomain();
		BeanUtil.copyProperties(role, roleDomain, BaseDomain.ignoreColumn());
		roleRepository.save(roleDomain);
	}

	public void updateRole(String roleId, RoleVo role) {
		if (CommonUtil.isAnyEmpty(roleId, role.getRoleName())) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_NULL, "角色ID、角色名称不能为空");
		}

		RoleDomain roleDomain = roleRepository.getRoleByRoleName(role.getRoleName());
		if (CommonUtil.isNotEmpty(roleDomain) && roleId.equals(roleDomain.getId())) {
			throw ServiceException.warn("该角色名称已经被使用");
		}

		RoleDomain domain = roleRepository.findOne(roleId);
		if (CommonUtil.isEmpty(domain)) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_FOUND, "角色数据");
		}

		BeanUtil.copyProperties(role, domain, BaseDomain.ignoreColumn());
		roleRepository.save(domain);
	}

	public void deleteRole(String roleId) {
		RoleDomain domain = roleRepository.findOne(roleId);
		if (CommonUtil.isEmpty(domain)) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_FOUND, "角色数据");
		}
		domain.setStatus(DataStatusDef.DISABLE);
		roleRepository.save(domain);
	}

	public RoleVo getRoleById(String roleId) {
		RoleDomain domain = roleRepository.findOne(roleId);
		if (CommonUtil.isEmpty(domain)) {
			throw ServiceException.warning(ErrorCode.DATA_NOT_FOUND, "角色数据");
		}
		RoleVo roleVo = new RoleVo();
		BeanUtil.copyProperties(domain, roleVo);
		return roleVo;
	}

	public PageInfo<RoleVo> query(PageParam page, String roleName) {
		PageRequest pageable = new PageRequest(page.getPage(), page.getSize(), new Sort(Direction.DESC, new String[] { DB.DEFAULT_SORT }));

		QRoleDomain qRoleDomain = QRoleDomain.roleDomain;
		BooleanExpression predicate = qRoleDomain.status.eq(DataStatusDef.ENABLE);
		if (StringUtil.notBlankOrNull(roleName)) {
			predicate = predicate.and(qRoleDomain.roleName.like(roleName));
		}

		Page<RoleDomain> pageContent = roleRepository.findAll(predicate, pageable);
		List<RoleVo> vos = new ArrayList<RoleVo>();
		pageContent.getContent().stream().forEach(role -> {
			RoleVo vo = new RoleVo();
			BeanUtil.copyProperties(role, vo);
			vos.add(vo);
		});

		return new PageInfo<RoleVo>(vos, pageContent.getTotalPages(), pageContent.getTotalElements());
	}

}
